<?php
require_once dirname(__FILE__) . '/includes/MiscUtils.class.php';
require_once dirname(__FILE__) . '/DbUtils.class.php';
require_once dirname(__FILE__) . '/metadata.php';
require_once dirname(__FILE__) . '/config.php';

$folder='';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);

$find = MiscUtils::getParam('find', '');
$cat = MiscUtils::getParam('cat', 0);
$from = MiscUtils::getParam('from', '');
$to = MiscUtils::getParam('to', '');

$cond_vals = new stdClass();
$cond_vals->c = '';
$cond_vals->v = array();
$cond_vals->c = "(t.name LIKE :v1 OR t.contact_name LIKE :v1 OR t.contact_phone LIKE :v1 OR t.company LIKE :v1)";
$cond_vals->v[":v1"] = "%$find%";

if ($cat == 0) {
    //$cond_vals->c .= " AND t.type = :v2";
    //$cond_vals->v[":v2"] = 0;
} else if ($cat == 1) {
    $cond_vals->c .= " AND t.type = :v2";
    $cond_vals->v[":v2"] = 0;
} else if ($cat == 2) {
    $cond_vals->c .= " AND t.type = :v2";
    $cond_vals->v[":v2"] = 1;
}

if ($from != '' && $to != '') {
    $cond_vals->c .= " AND (t.date >= :v3 AND t.date <= :v4)";
    $cond_vals->v[":v3"] = $from;
    $cond_vals->v[":v4"] = $to;
} else if ($from != '' && $to == '') {
    $cond_vals->c .= " AND t.date >= :v3";
    $cond_vals->v[":v3"] = $from;
} else if ($from == '' && $to != '') {
    $cond_vals->c .= " AND t.date <= :v3";
    $cond_vals->v[":v3"] = $to;
}

$page = MiscUtils::getParam('p', 1);
$order = MiscUtils::getParam('o', 't.name');
$queue = MiscUtils::getParam('q', 'DESC');

$opt = new stdClass();
$opt->select_expr = 'id,date,name,company,contact_name,contact_phone,type';
$items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_MODULE . 'data_customer', $cond_vals, $order, $queue, NULL, NULL, $opt);

$export_name = '客户供应商资料'.date('YmdHis');
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=$export_name.xls");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
    <style>
        * {
            font-family: Trebuchet MS;
            font-size: 12px;
            padding: 0;
            margin: 0;
        }
        table.list {
            border-left: 1px solid #666;
            border-top: 1px solid #666;
            font-size: 12px;
            width: 950px;
        }
        table.list th.sort {
            text-align: left;
            height: 28px;
            background-color: #BD9D9D;
            padding: 0 5px;
            border-right: 1px solid #666;
            border-bottom: 1px solid #666;
            font-weight: bold;
            vertical-align: middle;
            cursor: pointer;
            color: #000;
        }
        table.list tr.rowodd td {
            padding: 0 5px;
            height: 25px;
            background-color: #eee;
            border-right: 1px solid #666;
            border-bottom: 1px solid #666;
            word-wrap: break-word;
            vertical-align: middle;
        }
        table.list tr.roweven td {
            padding: 0 5px;
            height: 25px;
            background-color: #fff;
            border-right: 1px solid #666;
            border-bottom: 1px solid #666;
            word-wrap: break-word;
            vertical-align: middle;
        }
        table.list tr.rowodd:hover td {
            background-color: #f4f3b2;
        }
        table.list tr.roweven:hover td {
            background-color: #f4f3b2;
        }
        table.list tr.none td {
            padding: 0 5px;
            height: 25px;
            background-color: #fff;
            border-right: 1px solid #666;
            border-bottom: 1px solid #666;
            word-wrap: break-word;
        }
        table.list tr.current td {
            border-right: 1px solid #666;
            border-bottom: 1px solid #666;
            background-color: #ff8000;
            height: 25px;
            padding: 0 5px;
            word-wrap: break-word;
        }
        .chrome {
            -webkit-appearance: none;
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
        }
        .imgbtn {
            background-image: url(ext/plugin/bin/<?php echo $folder; ?>/assets/imgs/add.png);
            background-repeat: no-repeat;
            background-position: 5px 2px;
            padding: 5px 5px 5px 30px;
            border: 1px solid #666;
            background-color: #fff;
            cursor: pointer;
            border-radius: 3px;
        }
        .imgbtn:hover {
            background-color: #eee;
        }
        .imgico {
            cursor: pointer;
        }

        .imgexp {
            background-image: url(ext/plugin/bin/<?php echo $folder; ?>/assets/imgs/export_excel.png);
            background-repeat: no-repeat;
            background-position: 0px 2px;
            padding: 5px 5px 5px 30px;
            border: 1px solid #666;
            background-color: #fff;
            cursor: pointer;
            border-radius: 3px;
        }
        .imgexp:hover {
            background-color: #eee;
        }

        .searchwrapper {
            width:310px; /*follow your image's size*/
            height:40px;/*follow your image's size*/
            background-image:url(ext/plugin/bin/<?php echo $folder; ?>/assets/imgs/searchbox.jpg);
            background-repeat:no-repeat; /*important*/
            padding:0px;
            margin:0px;
            position:relative; /*important*/
        }
        .searchbox {
            border:0px; /*important*/
            background-color:transparent; /*important*/
            position:absolute; /*important*/
            top:4px;
            left:9px;
            width:256px;
            height:31px;
            color:#fff;
            outline: none;
        }
        .searchbox_submit {
            border:0px; /*important*/
            background-color:transparent; /*important*/
            position:absolute; /*important*/
            top:4px;
            left:265px;
            width:32px;
            height:32px;
        }
        .styled-select {
            width: 140px;
            height: 34px;
            overflow: hidden;
            background: url(ext/plugin/bin/<?php echo $folder; ?>/assets/imgs/down_arrow_select.jpg) no-repeat right #ddd;
            border: 1px solid #ccc;
            margin-left: 5px;
        }
        .styled-select select {
            background: transparent;
            width: 168px;
            padding: 5px;
            font-size: 16px;
            line-height: 1;
            border: 0;
            border-radius: 0;
            height: 34px;
            -webkit-appearance: none;
        }
        .styled-select option {
            padding: 5px;
            font-size: 16px;
            line-height: 1;
        }
        .drwrap {
            background: #ddd;
            border: 1px solid #666;
            height: 34px;
            margin-left: 5px;
            line-height: 32px;
            vertical-align: middle;
            font-family: Lucida Sans Unicode;
        }
        .drwrap input[type=text]{
            border: 1px solid #aaa;
            height: 23px;
            padding-left: 5px;
        }
    </style>
</head>
<body>
    <table class="list" style="margin-top: 5px;" cellPadding="0" cellSpacing="0" border="1">
        <tr>
            <th colspan="6" style="height: 40px;" align="center">客户供应商资料</th>
        </tr>
        <tr>
            <th class="sort" width="100px">名称</th>
            <th class="sort" width="100px">公司抬头</th>
            <th class="sort" width="80px">联系人</th>
            <th class="sort" width="80px" >电话</th>
            <th class="sort" width="80px" >类型</th>
            <th class="sort" width="80px">操作时间</th>
        </tr>
        <?php foreach ($items->d as $idx => &$item) { ?>
            <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>" >
                <td ><?php echo $item->name; ?></td>
                <td><?php echo $item->company; ?></td>
                <td><?php echo $item->contact_name; ?></td>
                <td><?php echo $item->contact_phone; ?></td>
                <td><?php echo ($item->type == 0) ? '客户资料' : '供应商资料'; ?></td>
                <td><?php echo$item->date; ?></td>
            </tr>
        <?php } ?>
        <?php if (count($items->d) == 0) { ?>
            <tr class="none" ><td colspan=9>没有记录</td></tr>
        <?php } ?>
    </table>

</body>
</html>